/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import cs4280.action.CinemaControl;
import cs4280.bean.Member;
import cs4280.dao.MemberDAO;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Yvonne
 */
public class memberControl extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*
        String jspPage = this.doIndexPage(request, response);
        this.dispatchJsp(jspPage, request, response);*/
        this.doPost(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String jspPage = "";
        HttpSession session = request.getSession();
        Member member = (Member) session.getAttribute("member");
        if (member != null) {
        requestControll requestC = new requestControll();
        requestC.prepareRefund(request, response);
            jspPage = "/WEB-INF/jsp/manageMember.jsp";
        } else {
            jspPage = "/WEB-INF/jsp/manageNonmember.jsp";
        }
        this.dispatchJsp(jspPage, request, response);
    }

    private void dispatchJsp(String jspPage, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher(jspPage).forward(request, response);
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet memberControl</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet memberControl at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {
            out.close();
        }
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    public String doPrepareRegisterPage(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        return "/WEB-INF/jsp/register.jsp";

    }

    public String doRegisterPage(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();

        if (!((String) request.getParameter("password")).equals((String) request.getParameter("password2"))) {
            request.setAttribute("msg", "Two passwords must be same!");

            request.setAttribute("newMember", null);

        } else {
            String role = "Customer";
            Member currentMember = (Member) session.getAttribute("member");

            if (currentMember != null && currentMember.getRole().equals("Manager")) {
                role = "Officer";
            }


            Member newMember = new Member();
            newMember.setUsername((String) request.getParameter("username"));
            newMember.setPassword((String) request.getParameter("password"));

            newMember.setLp(0);
            newMember.setRole(role);

            request.setAttribute("newMember", newMember);


            MemberDAO memberDAO = new MemberDAO();
            switch (memberDAO.addMember(newMember)) {


                case OK:
                    request.setAttribute("msg", "Registered successfully!");
                    //if member is managner, do not need to set
                    if (currentMember == null || !currentMember.getRole().equals("Manager")) {
                        session.setAttribute("member", memberDAO.getMember(newMember.getUsername()));
                    }
                    break;
                case DUPLICATE:
                    request.setAttribute("msg", "Member's username already exists!");
                    break;
                case FAIL:
                    request.setAttribute("msg", "Fail to register!");
            }
        }

        return "/WEB-INF/jsp/doRegister.jsp";

    }

    public String doPrepareUpdateMemberPage(HttpServletRequest request, HttpServletResponse response) {
        Member member = new Member();
        MemberDAO memberDAO = new MemberDAO();
        member = memberDAO.getMember(request.getParameter("username"));
        request.setAttribute("officer", member);


        return "/WEB-INF/jsp/memberUpdater.jsp";
    }

    public String doUpateMemberPage(HttpServletRequest request, HttpServletResponse response) {
        Member member = new Member();

        int lp = Integer.parseInt(request.getParameter("lp"));
        String role = request.getParameter("role");
        /*
        Member currentmember= CinemaControl.member;
        if(currentmember.getRole().equals("Manager")){

        lp = Integer.parseInt(request.getParameter("lp"));
        role = request.getParameter("role");

        }*/
        member.setUsername(request.getParameter("username"));
        member.setPassword(request.getParameter("password"));
        member.setLp(lp);
        member.setRole(role);



        request.setAttribute("officer", member);
        MemberDAO memberDAO = new MemberDAO();

        switch (memberDAO.updateMember(member)) {

            case OK:
                request.setAttribute("msg", "Update member successfully!");
                break;

            case FAIL:
                request.setAttribute("msg", "Fail to update member");


        }
        return "/WEB-INF/jsp/domemberUpdater.jsp";

    }

    public String doPrepareDeleteMemberPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Member d_member = new Member();
        MemberDAO movieDAO = new MemberDAO();
        d_member = movieDAO.getMember(request.getParameter("username"));
        request.setAttribute("d_member", d_member);


        return "/WEB-INF/jsp/memberDeleter.jsp";

    }

    public String doDeleteMemberPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Member d_member = new Member();
        MemberDAO memberDAO = new MemberDAO();


        d_member = memberDAO.getMember(request.getParameter("username"));
        request.setAttribute("d_member", d_member);

        switch (memberDAO.deleteMember(d_member)) {

            case OK:
                request.setAttribute("msg", "Delete member successfully!");
                break;

            case FAIL:
                request.setAttribute("msg", "Fail to delete member");
        }
        return "/WEB-INF/jsp/domemberDeleter.jsp";

    }

    public void doChangePassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        MemberDAO memberDAO = new MemberDAO();


        Member d_member = memberDAO.getMember(request.getParameter("username"));


        String password_n=request.getParameter("password");
        d_member.setPassword(password_n);
        switch (memberDAO.updateMember(d_member)) {

            case OK:
                request.setAttribute("msg", "The password has been updated successfully!");
                break;

            case FAIL:
                request.setAttribute("msg", "Fail to update, please try again");
        }



    }

}
